{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Create by 2020-09-24\n",
    "\n",
    "@author: zly\n",
    "\"\"\"\n",
    "import numpy as np\n",
    "from numpy import *\n",
    "from time import sleep\n",
    "import matplotlib\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "# 加载数据集\n",
    "def loadDataSet(fileName):\n",
    "    # 初始化一个空列表\n",
    "    dataSet = []\n",
    "    # 读取文件\n",
    "    fr = open(fileName)\n",
    "    # 循环遍历文件所有行\n",
    "    for line in fr.readlines():\n",
    "        # 切割每一行的数据\n",
    "        curLine = line.strip().split('\\t')\n",
    "        # 将数据转换为浮点类型,便于后面的计算\n",
    "        # fltLine = [float(x) for x in curLine]\n",
    "        # 将数据追加到dataMat\n",
    "        fltLine = list(map(float,curLine))    # 映射所有的元素为 float（浮点数）类型\n",
    "        dataSet.append(fltLine)\n",
    "    # 返回dataMat\n",
    "    return dataSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用欧式距离计算两个向量的距离\n",
    "def distEclud(vecA, vecB):\n",
    "    # 数组元素求平方\n",
    "    return sqrt(sum(power(vecA - vecB, 2)))\n",
    "\n",
    "# 构建一个包含k个随机质心的集合\n",
    "def randCent(dataMat, k):\n",
    "    # 获取样本数与特征值(80,2)\n",
    "    m, n = shape(dataMat)\n",
    "    # 初始化质心,创建(k,n)个以零填充的矩阵\n",
    "    centroids = mat(zeros((k, n)))\n",
    "    # 循环遍历特征值\n",
    "    for j in range(n):\n",
    "        '''\n",
    "        随机质心必须要整个数据集的边界之内\n",
    "        找到每个维的最大值和最小值，求出范围\n",
    "        然后生成0到1.0之间的随机数并通过最小值和取值范围，以便确保随机点在数据边界之内\n",
    "        '''\n",
    "        #计算每一列的最小值\n",
    "        minJ = min(dataMat[:, j])\n",
    "        #print(\"minJ\",minJ)\n",
    "        # 计算每一列的范围值\n",
    "        rangeJ = float(max(dataMat[:, j]) - minJ)\n",
    "        #print(\"rangeJ\",rangeJ)\n",
    "        # 计算每一列的质心,并将值赋给centroids\n",
    "        #print(\"mat(minJ + rangeJ * random.rand(k, 1))\",mat(minJ + rangeJ * random.rand(k, 1)))\n",
    "        #将最小值加上范围（max-min）*(3,1)矩阵\n",
    "        centroids[:, j] = mat(minJ + rangeJ * random.rand(k, 1))\n",
    "#         print('centroids[:, j]\\n',centroids[:, j])\n",
    "    #返回质心\n",
    "    return centroids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(80, 2)\n",
      "[[-5.379713]] [[4.838138]] [[-4.232586]] [[5.1904]]\n"
     ]
    }
   ],
   "source": [
    "dataMat=mat(loadDataSet('testSet.txt'))\n",
    "print(dataMat.shape)\n",
    "print(min(dataMat[:,0]),max(dataMat[:,0]),min(dataMat[:,1]),max(dataMat[:,1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAXRElEQVR4nO3db2hk53XH8d/peNfCMyld8EJC7HRj2kBN1llhIZX1izWr1Gs5atIXfeFtHELzwhQakpCGOM6aGiQaKIEkhATK0hYKMTKFuOtKRGidNmwpi+VoI8Wta8e4IW7sNHhDKam0SLszOX0hzVoazUhXM/fP89z7/cDA6ko788zcmTPnnufc55q7CwAQr18regAAgMEQyAEgcgRyAIgcgRwAIkcgB4DI3VLEg95+++1+7NixIh4aAKJ15cqVX7j70c7thQTyY8eOaWlpqYiHBoBomdnr3bZTW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HIAeAyBHIASByBHIAiByBHAAi9/+zIU6KlJOMnQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# zly_test\n",
    "dataMat_tmp=np.array(dataMat)\n",
    "plt.scatter(np.transpose(dataMat_tmp)[0], np.transpose(dataMat_tmp)[1],marker='.',color='k',s=100)     \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[-1.06595257, -1.33272598],\n",
       "        [-1.70664586,  1.99718872]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "randCent(dataMat, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "必选:\n",
    "dataMat:数据集\n",
    "k:簇数\n",
    "非必选:\n",
    "distMeas=distEclud ：距离(r)\n",
    "createCent=randCent：创建初始质心的函数\n",
    "返回:\n",
    "centroids:质心\n",
    "clusterAssment:\n",
    "clusterAssment包含两个列:\n",
    "第一列记录簇索引值,\n",
    "第二列存储误差(误差是指当前点到簇质心的距离,后面会使用该误差来评价聚类的效果)\n",
    "\"\"\"\n",
    "def kMeans(dataMat, k, distMeas=distEclud, createCent=randCent):\n",
    "    # 获取样本数和特征数（80,2）\n",
    "    m, n = shape(dataMat)\n",
    "    # 初始化一个矩阵来存储每个点的簇分配结果\n",
    "    # clusterAssment包含两个列:\n",
    "    #一列记录簇索引值,\n",
    "    #第二列存储误差(误差是指当前点到簇质心的距离,后面会使用该误差来评价聚类的效果)\n",
    "    clusterAssment = mat(zeros((m, 2)))\n",
    "    \n",
    "    # 创建质心,随机K个质心\n",
    "    centroids = createCent(dataMat, k)\n",
    "    # 初始化标志变量,用于判断迭代是否继续,如果True,则继续迭代\n",
    "    clusterChanged = True\n",
    "    zly=0\n",
    "    while clusterChanged:\n",
    "        clusterChanged = False\n",
    "        # 遍历所有数据找到距离每个点最近的质心,\n",
    "        # 可以通过对每个点遍历所有质心并计算点到每个质心的距离来完成\n",
    "        for i in range(m):\n",
    "            minDist = inf#最小距离设置为无穷大\n",
    "            minIndex = -1#最小质心的index(索引)设置为-1\n",
    "            for j in range(k):\n",
    "                # 计算数据点到质心的距离\n",
    "                # 计算距离是使用distMeas参数给出的距离公式,默认距离函数是distEclud\n",
    "                distJI = distMeas(centroids[j, :], dataMat[i, :])\n",
    "                # 如果距离比minDist(最小距离)还小,更新minDist(最小距离)和最小质心的index(索引)\n",
    "                if distJI < minDist:\n",
    "                    minDist = distJI\n",
    "                    minIndex = j\n",
    "                    zly=zly+1\n",
    "            # 如果任一点的簇分配结果发生改变,则更新clusterChanged标志\n",
    "            if clusterAssment[i, 0] != minIndex: clusterChanged = True\n",
    "            # 更新簇分配结果为最小质心的index(索引),minDist(最小距离)的平方\n",
    "            #更新属于哪个簇，离那个簇距离的平方是多少\n",
    "            clusterAssment[i, :] = minIndex, minDist ** 2\n",
    "        # print(centroids)\n",
    "        # 遍历所有质心并更新它们的取值\n",
    "#         print(clusterAssment)\n",
    "        for cent in range(k):\n",
    "            # 通过数据过滤来获得给定簇的所有点\n",
    "            \"\"\"\n",
    "            nonzero(clusterAssment[:, 0].A == cent)\n",
    "            nonzero函数是numpy中用于得到数组array中非零元素的位置（数组索引）的函数\n",
    "            [0]表示行索引\n",
    "            [1]表示列索引\n",
    "            \"\"\"\n",
    "            ptsInClust = dataMat[nonzero(clusterAssment[:, 0].A == cent)[0]]\n",
    "#             print(ptsInClust)\n",
    "            # 计算所有点的均值,axis=0表示沿矩阵的列方向进行均值计算\n",
    "            centroids[cent, :] = mean(ptsInClust, axis=0)\n",
    "        \n",
    "    print('zly:',zly)\n",
    "    #返回所有的类质心与点分配结果\n",
    "    return centroids, clusterAssment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-4.007257 -3.207066]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 4.208187  2.984927]\n",
      " [ 2.831667  1.574018]\n",
      " [-0.790153  3.343144]\n",
      " [ 2.336445  2.875106]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.096701  3.886007]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.542056  2.778832]\n",
      " [-1.736822  4.241041]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.332948  3.983102]\n",
      " [-1.616805  3.573188]\n",
      " [ 2.321395  3.154987]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [ 0.450614 -3.302219]\n",
      " [-3.156485  3.191137]\n",
      " [ 3.165506 -3.999838]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [-1.786345  2.554248]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [-1.688346  2.230267]\n",
      " [ 2.592976 -2.054368]\n",
      " [-2.679011  0.785119]\n",
      " [ 0.939512 -4.023563]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [ 2.83652  -2.658556]\n",
      " [-2.709034  2.923887]\n",
      " [ 3.367037 -3.184789]\n",
      " [-3.284816  3.273099]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [-4.786473  3.358547]\n",
      " [ 2.624081 -3.260715]\n",
      " [-2.513661  2.642162]\n",
      " [ 1.864375 -3.176309]\n",
      " [-2.562539  2.884438]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [-1.685703  2.939697]\n",
      " [ 3.031012 -3.620252]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [ 0.939512 -4.023563]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.837877 -3.253815]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 4.208187  2.984927]\n",
      " [ 2.831667  1.574018]\n",
      " [-0.790153  3.343144]\n",
      " [ 2.336445  2.875106]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.096701  3.886007]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.542056  2.778832]\n",
      " [-1.736822  4.241041]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.321395  3.154987]\n",
      " [ 4.196223  1.126677]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [ 0.450614 -3.302219]\n",
      " [-3.156485  3.191137]\n",
      " [ 3.165506 -3.999838]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [-1.786345  2.554248]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [-1.688346  2.230267]\n",
      " [ 2.592976 -2.054368]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [ 2.83652  -2.658556]\n",
      " [-2.709034  2.923887]\n",
      " [ 3.367037 -3.184789]\n",
      " [-3.284816  3.273099]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [-4.786473  3.358547]\n",
      " [ 2.624081 -3.260715]\n",
      " [-2.513661  2.642162]\n",
      " [ 1.864375 -3.176309]\n",
      " [-2.562539  2.884438]\n",
      " [ 3.491078 -3.947487]\n",
      " [-1.616805  3.573188]\n",
      " [ 2.280615 -2.559444]\n",
      " [-1.685703  2.939697]\n",
      " [ 3.031012 -3.620252]\n",
      " [-2.133863  3.093686]\n",
      " [ 4.668892 -2.562705]\n",
      " [-2.967647  2.848696]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [ 0.939512 -4.023563]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 4.208187  2.984927]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.336445  2.875106]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.096701  3.886007]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.542056  2.778832]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.321395  3.154987]\n",
      " [ 4.196223  1.126677]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [ 3.165506 -3.999838]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.790153  3.343144]\n",
      " [ 2.943496 -3.357075]\n",
      " [-1.786345  2.554248]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [-1.688346  2.230267]\n",
      " [ 2.592976 -2.054368]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [ 2.83652  -2.658556]\n",
      " [-2.709034  2.923887]\n",
      " [ 3.367037 -3.184789]\n",
      " [-3.284816  3.273099]\n",
      " [ 3.091414 -3.815232]\n",
      " [-1.736822  4.241041]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [-4.786473  3.358547]\n",
      " [ 2.624081 -3.260715]\n",
      " [-2.513661  2.642162]\n",
      " [ 1.864375 -3.176309]\n",
      " [-2.562539  2.884438]\n",
      " [ 3.491078 -3.947487]\n",
      " [-1.616805  3.573188]\n",
      " [ 2.280615 -2.559444]\n",
      " [-1.685703  2.939697]\n",
      " [ 3.031012 -3.620252]\n",
      " [-2.133863  3.093686]\n",
      " [ 4.668892 -2.562705]\n",
      " [-2.967647  2.848696]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [ 0.939512 -4.023563]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 1.864375 -3.176309]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 4.208187  2.984927]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.336445  2.875106]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.096701  3.886007]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.542056  2.778832]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.321395  3.154987]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.790153  3.343144]\n",
      " [ 2.943496 -3.357075]\n",
      " [-1.786345  2.554248]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [-1.688346  2.230267]\n",
      " [ 2.592976 -2.054368]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [ 2.83652  -2.658556]\n",
      " [-2.709034  2.923887]\n",
      " [ 3.367037 -3.184789]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [-4.786473  3.358547]\n",
      " [ 2.624081 -3.260715]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [ 3.491078 -3.947487]\n",
      " [-1.616805  3.573188]\n",
      " [ 2.280615 -2.559444]\n",
      " [-1.685703  2.939697]\n",
      " [ 3.031012 -3.620252]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [-0.39237  -3.963704]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [-2.121479 -4.232586]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 4.208187  2.984927]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.336445  2.875106]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.096701  3.886007]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.542056  2.778832]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.321395  3.154987]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.39237  -3.963704]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.39237  -3.963704]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "zly: 1192\n"
     ]
    }
   ],
   "source": [
    "myCentroids,clustAssing=kMeans(dataMat, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 2.65077367, -2.79019029],\n",
       "        [-3.53973889, -2.89384326],\n",
       "        [ 2.6265299 ,  3.10868015],\n",
       "        [-2.46154315,  2.78737555]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myCentroids"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test\n"
     ]
    }
   ],
   "source": [
    "#自己做的测试画图\n",
    "print('test')\n",
    "label_test=np.array(clustAssing[:,0],dtype = np.int)\n",
    "dataMat\n",
    "clustAssing.shape\n",
    "def showDataSet(datamat,label_test,myCentroids):\n",
    "    \"\"\"\n",
    "    数据可视化\n",
    "    Parameters:\n",
    "        dataMat - 数据矩阵\n",
    "        labelMat - 数据标签\n",
    "        myCentroids - 质点\n",
    "    Returns:\n",
    "        无\n",
    "    \"\"\"\n",
    "    dataMat=np.array(datamat)\n",
    "    n = np.unique(label_test)\n",
    "    myCentroids_np=np.array(myCentroids)\n",
    "    last_01=[]\n",
    "    for j in range(len(n)):\n",
    "        tem=[]\n",
    "        for i in range(len(dataMat)):\n",
    "            if label_test[i]==j:tem.append(dataMat[i])\n",
    "        last_01.append(tem)\n",
    "    last=np.array(last_01)\n",
    "    print((last).shape)\n",
    "    for i in range(len(last)):\n",
    "        plt.scatter(np.transpose(last[i])[0], np.transpose(last[i])[1])\n",
    "   \n",
    "    plt.scatter(np.transpose(myCentroids_np)[0], np.transpose(myCentroids_np)[1],marker='+',color='k',s=100)     \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAXk0lEQVR4nO3df4ydVZ3H8c+XmWpr0c6GlnT6gy1kkdBgE8jEsNTEhmpBS4EYYnSjYddN+AezbVdxYfmRCchCQiLUaGKa1YSNbLBRxK11QxEsG9bFMC1QxYqQqrTTsoxsOrpsK532u3/cudOZO/feee69z49znuf9ShqZp9N7z50xn3vu9/mec8zdBQCI11lFDwAA0BuCHAAiR5ADQOQIcgCIHEEOAJHrL+JJFy9e7KtWrSriqQEgWnv37v29uy9pvF5IkK9atUojIyNFPDUARMvMftfsOqUVAIgcQQ4AkSPIASByBDkARI4gB4DIEeQAEDmCHI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9HjycvkjPyL7n5N0WPpBDVypOlrkt4r6Ukze9HMvlH0gNIyeRP385KeUO3G346yhviktZI+K+nKyd/li5MzVgSIGTkARI4ZOQBEjiAHgMgR5AAQOYIcACJHkANA5AhyAIgcQQ4Akft/mY+9GuuiFGwAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(80, 2)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "showDataSet(dataMat, label_test,myCentroids)\n",
    "dataMat.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "#二分k-means算法\n",
    "\"\"\"\n",
    "算法原理:\n",
    "\n",
    "由于传统的KMeans算法的聚类结果易受到初始聚类中心点选择的影响，\n",
    "因此在传统的KMeans算法的基础上进行算法改进，对初始中心点选取比较严格，\n",
    "各中心点的距离较远，这就避免了初始聚类中心会选到一个类上，\n",
    "一定程度上克服了算法陷入局部最优状态。\n",
    "二分KMeans(Bisecting KMeans)算法的主要思想是：首先将所有点作为一个簇，\n",
    "然后将该簇一分为二。之后选择能最大限度降低聚类代价函数（也就是误差平方和）的簇划分为两个簇。\n",
    "以此进行下去，直到簇的数目等于用户给定的数目k为止。\n",
    "以上隐含的一个原则就是：因为聚类的误差平方和能够衡量聚类性能，\n",
    "该值越小表示数据点越接近于他们的质心，聚类效果就越好。\n",
    "所以我们就需要对误差平方和最大的簇进行再一次划分，\n",
    "因为误差平方和越大，表示该簇聚类效果越不好，\n",
    "越有可能是多个簇被当成了一个簇，所以我们首先需要对这个簇进行划分。\n",
    "\"\"\"\n",
    "\n",
    "def biKmeans(dataMat, k, distMeas=distEclud):\n",
    "    # 获取样本数和特征数（80,2）\n",
    "    m, n = shape(dataMat)\n",
    "    # 创建一个矩阵来存储数据集中每个点的簇分配结果及平方误差\n",
    "    clusterAssment = mat(zeros((m, 2)))\n",
    "    # 计算整个数据集的质心,并使用一个列表来保留所有的质心\n",
    "    centroid0 = mean(dataMat, axis=0).tolist()[0]\n",
    "    centList = [centroid0]\n",
    "    print('centList',centList)\n",
    "    #遍历数据集中所有点来计算每个点到质心的误差值\n",
    "    for j in range(m):\n",
    "        clusterAssment[j, 1] = distMeas(mat(centroid0), dataMat[j, :]) ** 2\n",
    "    #对簇不停的进行划分,直到得到想要的簇数目为止\n",
    "    while (len(centList) < k):\n",
    "        # 初始化最小SSE为无穷大,用于比较划分前后的SSE\n",
    "        lowestSSE = inf\n",
    "        # 通过考察簇列表中的值来获得当前簇的数目,遍历所有的簇来决定最佳的簇进行划分\n",
    "        for i in range(len(centList)):\n",
    "            #对每一个簇,将该簇中的所有点堪称一个小的数据集\n",
    "            ptsInCurrCluster = dataMat[nonzero(clusterAssment[:, 0].A == i)[0], :]\n",
    "#             print('ptsInCurrCluster',ptsInCurrCluster)\n",
    "#             (80,2)\n",
    "#             print('shape',mat(ptsInCurrCluster).shape)\n",
    "            #将ptsInCurrCluster输入到函数kMeans中进行处理,k=2,\n",
    "            #kMeans会生成两个质心(簇),同时给出每个簇的误差值\n",
    "            \"\"\"\n",
    "            centroids：质心\n",
    "            clusterAssment：\n",
    "            clusterAssment包含两个列:\n",
    "            第一列记录簇索引值,\n",
    "            第二列存储误差(误差是指当前点到簇质心的距离,后面会使用该误差来评价聚类的效果)\n",
    "            \"\"\"\n",
    "            centroidMat, splitClustAss = kMeans(ptsInCurrCluster, 2, distMeas)\n",
    "            #将误差值与剩余数据集的误差之和作为本次划分的误差\n",
    "            #比如说之前有100个数据，我们二分后产生两个质点，产生的误差和赋值给sseSplit\n",
    "            sseSplit = sum(splitClustAss[:, 1])\n",
    "            #将没有拆分的数据点进行误差求和，第一次是全部参加，所以第一次的时候sseNotSplit为0\n",
    "            #第二次，会将第一次已经二分的簇中，其中（2个簇）一个最值得切分进行切分，\n",
    "            #分别计算切分的和不要切分的簇的误差和，以此类推\n",
    "            sseNotSplit = sum(clusterAssment[nonzero(clusterAssment[:, 0].A != i)[0], 1])\n",
    "            print('sseSplit, and notSplit: ', sseSplit, sseNotSplit)\n",
    "            # 如果本次划分的SSE值最小,则本次划分被保存\n",
    "            if (sseSplit + sseNotSplit) < lowestSSE:\n",
    "                #得到值得二分的簇，记录该簇的编号\n",
    "                bestCentToSplit = i\n",
    "                #得到已经二分的那个簇的两个新的质心\n",
    "                bestNewCents = centroidMat\n",
    "                #包含两个数据：1，数据属于哪个簇，2误差\n",
    "                bestClustAss = splitClustAss.copy()\n",
    "                #将最小误差和赋值，用于for循环后面的比较操作（该循环体的if）\n",
    "                lowestSSE = sseSplit + sseNotSplit\n",
    "        \"\"\"\n",
    "        zly：下面代码通过上文for循环已经得到最值得切分的簇，但是还没有更新簇列表和对应的数据集中的属性\n",
    "        以下两个print意思是：\n",
    "        第一个：打印当前质心的个数\n",
    "        第二个：哪个簇的误差最大（最值得去二分）\n",
    "        第三个：最值得去二分的那个簇有多少数据\n",
    "        \"\"\"\n",
    "        # 找出最好的簇分配结果\n",
    "        # 调用kmeans函数并且指定簇数为2时,会得到两个编号分别为0和1的结果簇\n",
    "        bestClustAss[nonzero(bestClustAss[:, 0].A == 1)[0], 0] = len(centList)\n",
    "        print('len(centList)',len(centList))\n",
    "        # 更新为最佳质心\n",
    "        bestClustAss[nonzero(bestClustAss[:, 0].A == 0)[0], 0] = bestCentToSplit\n",
    "        print('the bestCentToSplit is: ', bestCentToSplit)\n",
    "        print('the len of bestClustAss is: ', len(bestClustAss))\n",
    "        \"\"\"\n",
    "        更新我们的质心，以下代码表示：\n",
    "        第一行代码：将值得分的那个簇（对应原簇那个list中，对应为i编号的簇）的质点更新为切分好的两个新簇的第一个质点\n",
    "        第二行代码：append新簇的第二个质点\n",
    "        \"\"\"\n",
    "        # 更新质心列表\n",
    "        # 更新原质心list中的第i个质心为使用二分kMeans后bestNewCents的第一个质心\n",
    "        centList[bestCentToSplit] = bestNewCents[0, :].tolist()[0]\n",
    "        # 添加bestNewCents的第二个质心\n",
    "        centList.append(bestNewCents[1, :].tolist()[0])\n",
    "        # 重新分配最好簇下的数据(质心)以及SSE\n",
    "        clusterAssment[nonzero(clusterAssment[:, 0].A == bestCentToSplit)[0], :] = bestClustAss\n",
    "    return mat(centList), clusterAssment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "centList [[-0.10361321250000004, 0.05430119999999998]]\n",
      "ptsInCurrCluster [[ 1.658985  4.285136]\n",
      " [-3.453687  3.424321]\n",
      " [ 4.838138 -1.151539]\n",
      " [-5.379713 -3.362104]\n",
      " [ 0.972564  2.924086]\n",
      " [-3.567919  1.531611]\n",
      " [ 0.450614 -3.302219]\n",
      " [-3.487105 -1.724432]\n",
      " [ 2.668759  1.594842]\n",
      " [-3.156485  3.191137]\n",
      " [ 3.165506 -3.999838]\n",
      " [-2.786837 -3.099354]\n",
      " [ 4.208187  2.984927]\n",
      " [-2.123337  2.943366]\n",
      " [ 0.704199 -0.479481]\n",
      " [-0.39237  -3.963704]\n",
      " [ 2.831667  1.574018]\n",
      " [-0.790153  3.343144]\n",
      " [ 2.943496 -3.357075]\n",
      " [-3.195883 -2.283926]\n",
      " [ 2.336445  2.875106]\n",
      " [-1.786345  2.554248]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [-3.403367 -2.778288]\n",
      " [ 1.778124  3.880832]\n",
      " [-1.688346  2.230267]\n",
      " [ 2.592976 -2.054368]\n",
      " [-4.007257 -3.207066]\n",
      " [ 2.257734  3.387564]\n",
      " [-2.679011  0.785119]\n",
      " [ 0.939512 -4.023563]\n",
      " [-3.674424 -2.261084]\n",
      " [ 2.046259  2.735279]\n",
      " [-3.18947   1.780269]\n",
      " [ 4.372646 -0.822248]\n",
      " [-2.579316 -3.497576]\n",
      " [ 1.889034  5.1904  ]\n",
      " [-0.798747  2.185588]\n",
      " [ 2.83652  -2.658556]\n",
      " [-3.837877 -3.253815]\n",
      " [ 2.096701  3.886007]\n",
      " [-2.709034  2.923887]\n",
      " [ 3.367037 -3.184789]\n",
      " [-2.121479 -4.232586]\n",
      " [ 2.329546  3.179764]\n",
      " [-3.284816  3.273099]\n",
      " [ 3.091414 -3.815232]\n",
      " [-3.762093 -2.432191]\n",
      " [ 3.542056  2.778832]\n",
      " [-1.736822  4.241041]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [-4.323818 -3.938116]\n",
      " [ 3.792121  5.135768]\n",
      " [-4.786473  3.358547]\n",
      " [ 2.624081 -3.260715]\n",
      " [-4.009299 -2.978115]\n",
      " [ 2.493525  1.96371 ]\n",
      " [-2.513661  2.642162]\n",
      " [ 1.864375 -3.176309]\n",
      " [-3.171184 -3.572452]\n",
      " [ 2.89422   2.489128]\n",
      " [-2.562539  2.884438]\n",
      " [ 3.491078 -3.947487]\n",
      " [-2.565729 -2.012114]\n",
      " [ 3.332948  3.983102]\n",
      " [-1.616805  3.573188]\n",
      " [ 2.280615 -2.559444]\n",
      " [-2.651229 -3.103198]\n",
      " [ 2.321395  3.154987]\n",
      " [-1.685703  2.939697]\n",
      " [ 3.031012 -3.620252]\n",
      " [-4.599622 -2.185829]\n",
      " [ 4.196223  1.126677]\n",
      " [-2.133863  3.093686]\n",
      " [ 4.668892 -2.562705]\n",
      " [-2.793241 -2.149706]\n",
      " [ 2.884105  3.043438]\n",
      " [-2.967647  2.848696]\n",
      " [ 4.479332 -1.764772]\n",
      " [-4.905566 -2.91107 ]]\n",
      "shape (80, 2)\n",
      "[[-3.453687  3.424321]\n",
      " [-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [ 0.450614 -3.302219]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-0.790153  3.343144]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [ 0.939512 -4.023563]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-1.736822  4.241041]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-1.616805  3.573188]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.133863  3.093686]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 2.668759  1.594842]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 4.208187  2.984927]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.336445  2.875106]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 2.257734  3.387564]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 2.096701  3.886007]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 3.542056  2.778832]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 2.321395  3.154987]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-0.790153  3.343144]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-1.736822  4.241041]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-1.616805  3.573188]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.133863  3.093686]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 2.668759  1.594842]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 4.208187  2.984927]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.336445  2.875106]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 2.257734  3.387564]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 2.096701  3.886007]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 3.542056  2.778832]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 2.321395  3.154987]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[-3.453687  3.424321]\n",
      " [-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-0.790153  3.343144]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-1.736822  4.241041]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-1.616805  3.573188]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.133863  3.093686]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 2.668759  1.594842]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 4.208187  2.984927]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.336445  2.875106]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 2.257734  3.387564]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 2.096701  3.886007]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 3.542056  2.778832]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 2.321395  3.154987]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "zly: 358\n",
      "sseSplit, and notSplit:  828.6926539968681 0.0\n",
      "len(centList) 1\n",
      "the bestCentToSplit is:  0\n",
      "the len of bestClustAss is:  80\n",
      "ptsInCurrCluster [[-3.453687  3.424321]\n",
      " [-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-0.790153  3.343144]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-1.736822  4.241041]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-1.616805  3.573188]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.133863  3.093686]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "shape (40, 2)\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-3.453687  3.424321]\n",
      " [-0.790153  3.343144]\n",
      " [-1.736822  4.241041]\n",
      " [-1.616805  3.573188]\n",
      " [-2.133863  3.093686]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "zly: 223\n",
      "sseSplit, and notSplit:  62.64959202653465 428.6946327807764\n",
      "ptsInCurrCluster [[ 1.658985  4.285136]\n",
      " [ 4.838138 -1.151539]\n",
      " [ 0.972564  2.924086]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 2.668759  1.594842]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 4.208187  2.984927]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.336445  2.875106]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 1.778124  3.880832]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 2.257734  3.387564]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.046259  2.735279]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 1.889034  5.1904  ]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 2.096701  3.886007]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 2.329546  3.179764]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 3.542056  2.778832]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 3.792121  5.135768]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.332948  3.983102]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 2.321395  3.154987]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 2.884105  3.043438]\n",
      " [ 4.479332 -1.764772]]\n",
      "shape (40, 2)\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 2.668759  1.594842]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 4.208187  2.984927]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.831667  1.574018]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 3.542056  2.778832]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 2.493525  1.96371 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.89422   2.489128]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.196223  1.126677]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "zly: 248\n",
      "sseSplit, and notSplit:  87.30471264989171 399.99802121609173\n",
      "len(centList) 2\n",
      "the bestCentToSplit is:  1\n",
      "the len of bestClustAss is:  40\n",
      "ptsInCurrCluster [[-3.453687  3.424321]\n",
      " [-5.379713 -3.362104]\n",
      " [-3.567919  1.531611]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.156485  3.191137]\n",
      " [-2.786837 -3.099354]\n",
      " [-2.123337  2.943366]\n",
      " [-0.39237  -3.963704]\n",
      " [-0.790153  3.343144]\n",
      " [-3.195883 -2.283926]\n",
      " [-1.786345  2.554248]\n",
      " [-3.403367 -2.778288]\n",
      " [-1.688346  2.230267]\n",
      " [-4.007257 -3.207066]\n",
      " [-2.679011  0.785119]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.18947   1.780269]\n",
      " [-2.579316 -3.497576]\n",
      " [-0.798747  2.185588]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.709034  2.923887]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.284816  3.273099]\n",
      " [-3.762093 -2.432191]\n",
      " [-1.736822  4.241041]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.786473  3.358547]\n",
      " [-4.009299 -2.978115]\n",
      " [-2.513661  2.642162]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.562539  2.884438]\n",
      " [-2.565729 -2.012114]\n",
      " [-1.616805  3.573188]\n",
      " [-2.651229 -3.103198]\n",
      " [-1.685703  2.939697]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.133863  3.093686]\n",
      " [-2.793241 -2.149706]\n",
      " [-2.967647  2.848696]\n",
      " [-4.905566 -2.91107 ]]\n",
      "shape (40, 2)\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "zly: 120\n",
      "sseSplit, and notSplit:  62.64959202653465 87.30471264989171\n",
      "ptsInCurrCluster [[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "shape (20, 2)\n",
      "[[0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [2.831667 1.574018]\n",
      " [2.046259 2.735279]\n",
      " [2.493525 1.96371 ]\n",
      " [4.196223 1.126677]]\n",
      "[[1.658985 4.285136]\n",
      " [4.208187 2.984927]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "[[2.668759 1.594842]\n",
      " [2.831667 1.574018]\n",
      " [2.046259 2.735279]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [4.196223 1.126677]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [4.208187 2.984927]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "[[2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [4.196223 1.126677]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "[[2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [4.196223 1.126677]]\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [2.884105 3.043438]]\n",
      "zly: 134\n",
      "sseSplit, and notSplit:  20.42056248813446 450.98424790755917\n",
      "ptsInCurrCluster [[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "shape (20, 2)\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.280615 -2.559444]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.280615 -2.559444]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.280615 -2.559444]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "zly: 100\n",
      "sseSplit, and notSplit:  32.82523163228564 436.3165071745162\n",
      "len(centList) 3\n",
      "the bestCentToSplit is:  0\n",
      "the len of bestClustAss is:  40\n",
      "ptsInCurrCluster [[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-1.736822  4.241041]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "shape (20, 2)\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-1.736822  4.241041]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]\n",
      " [-2.967647  2.848696]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]]\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-2.709034  2.923887]\n",
      " [-1.736822  4.241041]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]\n",
      " [-2.967647  2.848696]]\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-1.736822  4.241041]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]\n",
      " [-2.967647  2.848696]]\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-1.736822  4.241041]\n",
      " [-2.562539  2.884438]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.967647  2.848696]]\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-1.736822  4.241041]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-2.967647  2.848696]]\n",
      "[[-2.123337  2.943366]\n",
      " [-0.790153  3.343144]\n",
      " [-1.786345  2.554248]\n",
      " [-1.688346  2.230267]\n",
      " [-0.798747  2.185588]\n",
      " [-1.736822  4.241041]\n",
      " [-1.616805  3.573188]\n",
      " [-1.685703  2.939697]\n",
      " [-2.133863  3.093686]]\n",
      "[[-3.453687  3.424321]\n",
      " [-3.567919  1.531611]\n",
      " [-3.156485  3.191137]\n",
      " [-2.679011  0.785119]\n",
      " [-3.18947   1.780269]\n",
      " [-2.709034  2.923887]\n",
      " [-3.284816  3.273099]\n",
      " [-4.786473  3.358547]\n",
      " [-2.513661  2.642162]\n",
      " [-2.562539  2.884438]\n",
      " [-2.967647  2.848696]]\n",
      "zly: 176\n",
      "sseSplit, and notSplit:  16.95450913092986 119.90595551484286\n",
      "ptsInCurrCluster [[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [3.792121 5.135768]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [3.332948 3.983102]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "shape (20, 2)\n",
      "[[1.658985 4.285136]\n",
      " [0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[4.208187 2.984927]\n",
      " [1.889034 5.1904  ]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]]\n",
      "[[0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [1.778124 3.880832]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [2.096701 3.886007]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[1.658985 4.285136]\n",
      " [4.208187 2.984927]\n",
      " [1.889034 5.1904  ]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]]\n",
      "[[0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[1.658985 4.285136]\n",
      " [1.778124 3.880832]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]]\n",
      "[[0.972564 2.924086]\n",
      " [2.668759 1.594842]\n",
      " [4.208187 2.984927]\n",
      " [2.831667 1.574018]\n",
      " [2.336445 2.875106]\n",
      " [2.257734 3.387564]\n",
      " [2.046259 2.735279]\n",
      " [2.329546 3.179764]\n",
      " [3.542056 2.778832]\n",
      " [2.493525 1.96371 ]\n",
      " [2.89422  2.489128]\n",
      " [2.321395 3.154987]\n",
      " [4.196223 1.126677]\n",
      " [2.884105 3.043438]]\n",
      "[[1.658985 4.285136]\n",
      " [1.778124 3.880832]\n",
      " [1.889034 5.1904  ]\n",
      " [2.096701 3.886007]\n",
      " [3.792121 5.135768]\n",
      " [3.332948 3.983102]]\n",
      "zly: 101\n",
      "sseSplit, and notSplit:  21.81887228878202 113.635818718002\n",
      "ptsInCurrCluster [[ 4.838138 -1.151539]\n",
      " [ 0.450614 -3.302219]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "shape (20, 2)\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 2.592976 -2.054368]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 1.864375 -3.176309]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 2.280615 -2.559444]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.280615 -2.559444]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "[[ 0.450614 -3.302219]\n",
      " [ 0.704199 -0.479481]\n",
      " [ 2.190101 -1.90602 ]\n",
      " [ 0.939512 -4.023563]\n",
      " [ 2.127073 -2.98368 ]\n",
      " [ 1.864375 -3.176309]\n",
      " [ 2.280615 -2.559444]]\n",
      "[[ 4.838138 -1.151539]\n",
      " [ 3.165506 -3.999838]\n",
      " [ 2.943496 -3.357075]\n",
      " [ 2.592976 -2.054368]\n",
      " [ 4.372646 -0.822248]\n",
      " [ 2.83652  -2.658556]\n",
      " [ 3.367037 -3.184789]\n",
      " [ 3.091414 -3.815232]\n",
      " [ 2.624081 -3.260715]\n",
      " [ 3.491078 -3.947487]\n",
      " [ 3.031012 -3.620252]\n",
      " [ 4.668892 -2.562705]\n",
      " [ 4.479332 -1.764772]]\n",
      "zly: 173\n",
      "sseSplit, and notSplit:  32.82523163228564 98.96807798495898\n",
      "ptsInCurrCluster [[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-2.579316 -3.497576]\n",
      " [-3.837877 -3.253815]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-4.599622 -2.185829]\n",
      " [-2.793241 -2.149706]\n",
      " [-4.905566 -2.91107 ]]\n",
      "shape (20, 2)\n",
      "[[-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-3.403367 -2.778288]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.195883 -2.283926]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "[[-2.786837 -3.099354]\n",
      " [-0.39237  -3.963704]\n",
      " [-2.579316 -3.497576]\n",
      " [-2.121479 -4.232586]\n",
      " [-3.171184 -3.572452]\n",
      " [-2.565729 -2.012114]\n",
      " [-2.651229 -3.103198]\n",
      " [-2.793241 -2.149706]]\n",
      "[[-5.379713 -3.362104]\n",
      " [-3.487105 -1.724432]\n",
      " [-3.195883 -2.283926]\n",
      " [-3.403367 -2.778288]\n",
      " [-4.007257 -3.207066]\n",
      " [-3.674424 -2.261084]\n",
      " [-3.837877 -3.253815]\n",
      " [-3.762093 -2.432191]\n",
      " [-4.323818 -3.938116]\n",
      " [-4.009299 -2.978115]\n",
      " [-4.599622 -2.185829]\n",
      " [-4.905566 -2.91107 ]]\n",
      "zly: 95\n",
      "sseSplit, and notSplit:  18.398749985455712 117.3530618114752\n",
      "len(centList) 4\n",
      "the bestCentToSplit is:  2\n",
      "the len of bestClustAss is:  20\n"
     ]
    }
   ],
   "source": [
    "myCentroids01,clustAssing01=biKmeans(dataMat,5, distMeas=distEclud)\n",
    "label_test_01=np.array(clustAssing01[:,0],dtype = np.int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD4CAYAAADxeG0DAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8li6FKAAAZDElEQVR4nO3df2xcV5UH8O+pnTZWAolKHNn50U3bbUOiEildi9IGLSZhk0KapkIIwQKqlpX6x4LWyUK6ZNtGpgUVEYnWCCQULWgrEehWUJoNYZWWtgkqlAonadOWNP0R2ia2o7rt2pBgJxn77B/jZ3vGb2bezLvv3Xvf+36kKPXz+M2dGM7cOffcc0VVQURE/rrI9gCIiCgeBnIiIs8xkBMReY6BnIjIcwzkRESea7bxpAsWLNBly5bZeGoiIm8dOnTobVVtLb9uJZAvW7YMvb29Np6aiMhbIvJG2HWmVoiIPMdATkTkOQZyIiLPMZATEXmOgZyIyHMM5EREnmMgJ8q5fSf2Yf3P1mPVA6uw/mfrse/EPttDojpZqSMnIjfsO7EP3b/rxujYKABg4OwAun/XDQDYeMVGiyOjenBGTpRjPYd7JoN4YHRsFD2HeyyNyLw8fOLgjJwox06fPV3Xdd/k5RMHZ+REOdY2p62u677JwycOgIGcKNe6ru3C7KbZJddmN81G17VdlkZkVtY/cQSYWiHKsSC90HO4B6fPnkbbnDZ0XduVmbRD25w2DJwdCL2eJQzkRDm38YqNmQnc5bqu7SrJkQPZ+sQRYC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K0AbgKwTrO3OeEUgKXTvl4CoN/SWBInIrNQDOK7VfVh2+NJyBoAN4vIJwDMBvBeEfmxqn7e8rgawg1BKRCR1wF0qKovHdbqIiI3AvgOgI+o6qDt8ZgmIs0oLuKuA9AH4A8A/lFVX7Q6sARIcebxAIB3VXWL7fGkYWJG/lVVvcn2WBrFHDmZ8D0A7wHwmIg8KyI/sD0gkyYWcr8MYD+Ki38PZTGIT1gD4AsA1k78Lp+dmLWSwzgjJyLyHGfkRESeYyAnIvIcAzkRkecYyImIPMdATkTkOQZyIiLPMZATEXnu/wGuo7fA6vH0/AAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "showDataSet(dataMat, label_test_01,myCentroids01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#以下不看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "def distSLC(vecA, vecB):\n",
    "    '''\n",
    "    返回地球表面两点间的距离,单位是英里\n",
    "    给定两个点的经纬度,可以使用球面余弦定理来计算亮点的距离\n",
    "    :param vecA:\n",
    "    :param vecB:\n",
    "    :return:\n",
    "    '''\n",
    "    # 经度和维度用角度作为单位,但是sin()和cos()以弧度为输入.\n",
    "    # 可以将江都除以180度然后再诚意圆周率pi转换为弧度\n",
    "    a = sin(vecA[0, 1] * pi / 180) * sin(vecB[0, 1] * pi / 180)\n",
    "    b = cos(vecA[0, 1] * pi / 180) * cos(vecB[0, 1] * pi / 180) * \\\n",
    "        cos(pi * (vecB[0, 0] - vecA[0, 0]) / 180)\n",
    "    return arccos(a + b) * 6371.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [],
   "source": [
    "def clusterClubs(fileName, imgName, numClust=5):\n",
    "    # 创建一个空列表\n",
    "    datList = []\n",
    "    # 打开文本文件获取第4列和第5列,这两列分别对应维度和经度,然后将这些值封装到datList\n",
    "    for line in open(fileName).readlines():\n",
    "        lineArr = line.split('\\t')\n",
    "        datList.append([float(lineArr[4]), float(lineArr[3])])\n",
    "    datMat = mat(datList)\n",
    "    # 调用biKmeans并使用distSLC函数作为聚类中使用的距离计算方式\n",
    "    myCentroids, clustAssing = biKmeans(datMat, numClust, distMeas=distSLC)\n",
    "    # 创建一幅图和一个举行,使用该矩形来决定绘制图的哪一部分\n",
    "    fig = plt.figure()\n",
    "    rect = [0.1, 0.1, 0.8, 0.8]\n",
    "    # 构建一个标记形状的列表用于绘制散点图\n",
    "    scatterMarkers = ['s', 'o', '^', '8', 'p', 'd', 'v', 'h', '>', '<']\n",
    "    axprops = dict(xticks=[], yticks=[])\n",
    "    ax0 = fig.add_axes(rect, label='ax0', **axprops)\n",
    "    # 使用imread函数基于一幅图像来创建矩阵\n",
    "    imgP = plt.imread(imgName)\n",
    "    # 使用imshow绘制该矩阵\n",
    "    ax0.imshow(imgP)\n",
    "    # 再同一幅图上绘制一张新图,允许使用两套坐标系统并不做任何缩放或偏移\n",
    "    ax1 = fig.add_axes(rect, label='ax1', frameon=False)\n",
    "    # 遍历每一个簇并将它们一一画出来,标记类型从前面创建的scatterMarkers列表中得到\n",
    "    for i in range(numClust):\n",
    "        ptsInCurrCluster = datMat[nonzero(clustAssing[:, 0].A == i)[0], :]\n",
    "        # 使用索引i % len(scatterMarkers)来选择标记形状,这意味这当有更多簇时,可以循环使用这标记\n",
    "        markerStyle = scatterMarkers[i % len(scatterMarkers)]\n",
    "        # 使用十字标记来表示簇中心并在图中显示\n",
    "        ax1.scatter(ptsInCurrCluster[:, 0].flatten().A[0], ptsInCurrCluster[:, 1].flatten().A[0], \n",
    "                    marker=markerStyle,s=90)\n",
    "    ax1.scatter(myCentroids[:, 0].flatten().A[0], myCentroids[:, 1].flatten().A[0], marker='+', s=300)\n",
    "    plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
